//
//  VSFindGiftWaterLayout.m
//  UI项目2.0
//
//  Created by dlios on 15/7/16.
//  Copyright (c) 2015年 星际科技. All rights reserved.
//

#import "VSFindGiftWaterLayout.h"

@interface VSFindGiftWaterLayout ()

@property (nonatomic, retain) NSMutableArray *heights;
@property (nonatomic, retain) NSMutableArray *originX;

@end

@implementation VSFindGiftWaterLayout


- (void)dealloc
{
    [_heights release];
    [_originX release];
    [super dealloc];
}

- (void)setLineCount:(NSInteger)lineCount {
    if (lineCount == 0) {
        _lineCount = 0;
    }
    _lineCount = lineCount;
}

- (void)prepareLayout {
    [super prepareLayout];
    
    self.delegate = (id<WaterLayoutDelegate>)self.collectionView.delegate;
    
    [self resetHeightAndOringinXArray];
}

- (CGSize)collectionViewContentSize {
    
    CGFloat heightest = [self getHeightest:self.heights];
    
    CGSize size = CGSizeMake(0, heightest);
    
    [self resetHeightAndOringinXArray];
    return size;
}



- ( UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:( NSIndexPath *)indexPath {

    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGSize size = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
    
    CGRect rect = attributes.frame;
    
    rect.size.height = size.height;
    rect.size.width = (SCREEN_WIDTH - self.sectionInset.left - self.sectionInset.right - (self.lineCount - 1) * self.horizontalSpacing) / self.lineCount;
    
     NSInteger index = [self getLowerHeightRowIndex:self.heights];
    
    rect.origin.x = [self.originX[index] doubleValue];
    rect.origin.y = [self.heights[index] doubleValue];
    
    
    CGFloat newHeight = rect.origin.y + rect.size.height + self.verticalSpacing;
    [self.heights replaceObjectAtIndex:index withObject:[NSString stringWithFormat:@"%f", newHeight]];
    
    attributes.frame = rect;
    
    return attributes;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSArray *arr = [super layoutAttributesForElementsInRect:rect];
    if ([arr count] > 0) {
        return arr;
    }
    NSMutableArray *attributes = [NSMutableArray array];
    for (NSInteger i = 0 ; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
    return attributes;
}


#pragma mark - 获取高度最低的列下标

- (NSInteger)getLowerHeightRowIndex:(NSMutableArray *)height {
    
    NSInteger index = 0;
    
    if (height.count > 0) {
        CGFloat heightest = [height[0] doubleValue];
        
        for (NSInteger i = 1; i < height.count; i++) {
            if ([height[i] doubleValue] < heightest) {
                index = i;
            }
        }
        
        return index;
    }
    
    return 0;
}

#pragma mark - 获取高度最高列的高度
- (CGFloat)getHeightest:(NSMutableArray *)height {
    
    CGFloat heightest = self.sectionInset.top;
    if (height.count > 0) {
        for (NSInteger i = 0; i < height.count; i++) {
            if ([height[i] doubleValue] > heightest) {
                heightest = [height[i] doubleValue];
            }
        }
    }
    return heightest;
}

#pragma mark - 重置高度, X坐标数组
- (void)resetHeightAndOringinXArray {
    
    if (self.heights) {
        [self.heights removeAllObjects];
    } else {
        self.heights = [NSMutableArray array];
    }
    
    if (self.originX) {
        [self.originX removeAllObjects];
    } else {
        self.originX = [NSMutableArray array];
    }
    CGFloat itemWidth = (SCREEN_WIDTH - self.sectionInset.left - self.sectionInset.right - (self.lineCount - 1) * self.horizontalSpacing) / self.lineCount;
    for (NSInteger i = 0; i < self.lineCount; i++) {
        [self.heights addObject:[NSString stringWithFormat:@"%f", self.sectionInset.top]];
        CGFloat oringin_x = 0.0;
        if (i == 0) {
            oringin_x = self.sectionInset.left;
        } else {
            oringin_x = [self.originX[i - 1] doubleValue] + itemWidth + self.horizontalSpacing;
        }
        [self.originX addObject:[NSString stringWithFormat:@"%f", oringin_x]];
    }
}


@end
